package com.fileshare.web.service.impl;


import com.fileshare.web.constant.Status;
import com.fileshare.web.dao.CommentRepository;
import com.fileshare.web.dao.UserRepository;
import com.fileshare.web.dto.Bo.CommentBo;
import com.fileshare.web.dto.Vo.AllArticleCommentVo;
import com.fileshare.web.dto.Vo.ArticleCommentVo;
import com.fileshare.web.dto.Vo.CommentVo;
import com.fileshare.web.entity.Comment;
import com.fileshare.web.entity.User;
import com.fileshare.web.exception.FsRuntimeException;
import com.fileshare.web.service.CommentService;
import com.fileshare.web.utils.SystemUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.fileshare.web.constant.EntityConstant.ARTICLE_REFERENCE_TYPE;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentRepository commentRepository;
    @Resource
    private UserRepository userRepository;

    @Override
    public CommentVo createComment(HttpServletRequest request, CommentBo commentBo) {
        Comment comment = new Comment();
        User user = SystemUtil.getLoginUser();
        if (Objects.isNull(user)) {
            throw new FsRuntimeException("用户未登录");
        }
        comment.setMessage(commentBo.getMessage());
        //登录用户创建得得评论
        comment.setReferenceKey(commentBo.getReferenceId());
        comment.setReferenceType(ARTICLE_REFERENCE_TYPE);
        comment.setToReference(commentBo.getToReferenceId());
        comment.setDislikes(0);
        comment.setLikes(0);
        comment.setStatus(Status.Comment.PUBLISH);
        //第一次发布没有相关得评论
        Date now = new Date();
        comment.setCreateAt(now);
        comment.setUpdateAt(now);
        comment.setCreateBy(user.getUserSeq());
        comment.setUpdateBy(user.getUserSeq());
        comment.setCommentSeq(commentRepository.generateSeq());
        commentRepository.createComment(comment);
        return CommentVo.fromEntity(comment);
    }

    private ArticleCommentVo convertToArticleCommentVo(Comment comment) {
        ArticleCommentVo articleCommentVo = new ArticleCommentVo();
        articleCommentVo.setCreateAt(comment.getCreateAt());
        articleCommentVo.setCommentContent(comment.getMessage());
        articleCommentVo.setCommentSeq(comment.getCommentSeq());
        articleCommentVo.setCreateBy(comment.getCreateBy());
        return articleCommentVo;
    }

    //查找该评论下所有的子类
    public List<ArticleCommentVo> searchChildrenArticleCommentVo(String articleSeq, Comment parent, List<ArticleCommentVo> children) {
        //通过parent得到该parent下的children
        // 根据articleSeq 和to_reference == parent.CommentSeq
        List<Comment> childComments = commentRepository.searchCommentByArticleSeqAndToReference(articleSeq, parent.getCommentSeq());
        for (Comment childrenComment : childComments) {
            ArticleCommentVo childrenArticleCommentVo = convertToArticleCommentVo(childrenComment);
            User user = userRepository.queryUserBySeq(childrenComment.getCreateBy());
            childrenArticleCommentVo.setUserAvatar(user.getAvatar());
            childrenArticleCommentVo.setUserName(user.getUserName());
            children.add(childrenArticleCommentVo);
            searchChildrenArticleCommentVo(articleSeq, childrenComment, children);
        }
        return children;
    }

    //递归的写法
    @Override
    public List<AllArticleCommentVo> searchCommentByArticleSeq(String articleSeq, String articleReferenceType) {
        //组装comment
        List<Comment> comments = commentRepository.searchCommentByArticleSeq(articleSeq, articleReferenceType);
        //1.先找出第一层，先找出to_reference为空的comments
        List<Comment> allComments = comments.stream().filter(comment -> comment.getToReference() == null).collect(Collectors.toList());
        List<AllArticleCommentVo> articleCommentVos = new ArrayList<>();
        for (Comment parentComment : allComments) {
            AllArticleCommentVo curLevelComments = new AllArticleCommentVo();
            ArticleCommentVo articleVos = convertToArticleCommentVo(parentComment);
            //找到第一层发表评论用户头像，昵称
            //通过评论createAt(相当于user的seq）找到user的用户信息
            User user = userRepository.queryUserBySeq(parentComment.getCreateBy());
            articleVos.setUserAvatar(user.getAvatar());
            articleVos.setUserName(user.getUserName());
            curLevelComments.setCurrentComment(articleVos);
            List<ArticleCommentVo> childes = new ArrayList<>();
            curLevelComments.setChildCommentVos(searchChildrenArticleCommentVo(articleSeq, parentComment, childes));
            articleCommentVos.add(curLevelComments);
        }
        return articleCommentVos;
    }
}
